package com.yh.workbill.ui.mvp;

import android.util.Log;

import com.yh.workbill.base.IBaseModel;
import com.yh.workbill.db.CompanyDao;
import com.yh.workbill.db.UnitCompany;
import com.yh.workbill.db.UnitCompanyDao;
import com.yh.workbill.model.Company;
import com.yh.workbill.model.UseCompany;
import com.yh.workbill.model.WorkBill;
import com.yh.workbill.network.BaseCallback;
import com.yh.workbill.network.OkHttpService;

import java.util.List;

import androidx.arch.core.executor.ArchTaskExecutor;

public class MainModel {


    interface CompanyCallback extends IBaseModel.Fail {
        void SuccessCompany(List<Company> data);
    }

    interface WorkBillCallback extends IBaseModel.Fail {
        void SuccessWorkBill(List<WorkBill> data);
    }

    interface UnitCompanyCallback extends IBaseModel.Fail {
        void SuccessUnitCompany(List<String> data);
    }

    public void getAllCompany(CompanyDao dao, String user_uuid, CompanyCallback callback) {
        OkHttpService.getInstance().getAllCompany(user_uuid, 1, 500, new BaseCallback<List<Company>>() {
            @Override
            public void accessSuccess(int code, String msg, List<Company> data) {
                if (code == 200) {
                    //获取数据保存到数据库
                    ArchTaskExecutor.getIOThreadExecutor().execute(new Runnable() {
                        @Override
                        public void run() {
                            for (Company company : data) {
                                com.yh.workbill.db.Company db_company = dao.getCompany(company.getCompany_name());
                                if (db_company == null) {
                                    dao.add(new com.yh.workbill.db.Company(company.getCompany_name(), company.getCompany_address(), company.getCompany_phone()));
                                }
                            }
                        }
                    });
                }
            }

            @Override
            public void accessFailure(String errorMessage) {
                callback.fail(errorMessage);
            }
        });
    }

    public void getAllUseCompany(UnitCompanyDao dao, String user_uuid, CompanyCallback callback) {
        OkHttpService.getInstance().getAllUnitCompany(user_uuid, 1, 500, new BaseCallback<List<UseCompany>>() {
            @Override
            public void accessSuccess(int code, String msg, List<UseCompany> data) {
                if (code == 200) {
                    //获取数据保存到数据库
                    ArchTaskExecutor.getIOThreadExecutor().execute(new Runnable() {
                        @Override
                        public void run() {
                            for (UseCompany useCompany : data) {
                                UnitCompany unitComany = dao.getUnitComany(useCompany.getWork_use_company());
                                if (unitComany == null) {
                                    dao.add(new UnitCompany(useCompany.getWork_use_company()));
                                }
                            }
                        }
                    });
                }
            }

            @Override
            public void accessFailure(String errorMessage) {

            }
        });
    }


}
